//
//  ExpertInfoEditViewController.m
//  DaJiang
//
//  Created by sally on 16/10/6.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "CellManager.h"
#import "ExpertInfoEditViewController.h"
#import "LevelModel.h"
#import "MainModel.h"
#import "OtherModel.h"
#import "PickerView.h"
#import "SendWeiboImage.h"
#import "SendWeiboImageCell.h"
#import "ThemeEditViewController.h"
#import "DemoImageEditor.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface ExpertInfoEditViewController () <UITextFieldDelegate, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UITextViewDelegate,PickerViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIButton *headBtn;

@property (strong, nonatomic) NSMutableArray *tableData;
@property (strong, nonatomic) NSMutableArray *yearArr;
@property (strong, nonatomic) NSMutableArray *eduArr;
@property (strong, nonatomic) NSMutableArray *locArr;
@property (strong, nonatomic) NSMutableArray *level1Arr;
@property (strong, nonatomic) NSMutableArray *level2Arr;
@property (strong, nonatomic) NSMutableArray *idCardImageArr;        //身份证图片列表
@property (strong, nonatomic) NSMutableArray *qualificationImageArr; //资质图片列表
@property (strong, nonatomic) UIImage *headImage;                    //头像图片
@property (assign, nonatomic) ImageCell_Type imageType;              //选择图片类型

@property (strong, nonatomic) BecomeExpertModel *becomeExpertModel;
@property (strong, nonatomic) PickerView *yearPicker; //年限选择器
@property (strong, nonatomic) PickerView *eduPicker;  //学历选择器
@property (strong, nonatomic) PickerView *locPicker;  //地区选择器
@property (strong, nonatomic) PickerView *sortPicker; //专家分类选择器
@property(nonatomic,strong) DemoImageEditor *imageEditor;
@property(nonatomic,strong) ALAssetsLibrary *library;

@end

@implementation ExpertInfoEditViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self setNotification];
  [self initControl];
  [self initData];
  [self loadData];
}

- (void)dealloc {
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark 设置通知
- (void)setNotification {
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:) name:UITextFieldTextDidChangeNotification object:nil];
}

#pragma mark 初始化控件
- (void)initControl {
  self.title = @"成为专家";
  self.headerView.height = GExpertImageHeight;
  [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

  //TODO: 是否加入修改页面的逻辑？-暂不管
}

#pragma mark 初始化数据
- (void)initData {
  self.becomeExpertModel = [[BecomeExpertModel alloc] initWithNull];
  self.tableData = [NSMutableArray array];
  self.yearArr = [[NSMutableArray alloc] init];
  self.eduArr = [[NSMutableArray alloc] init];
  self.locArr = [[NSMutableArray alloc] init];
  self.level1Arr = [[NSMutableArray alloc] init];
  self.level2Arr = [[NSMutableArray alloc] init];
  self.idCardImageArr = [[NSMutableArray alloc] init];
  self.qualificationImageArr = [[NSMutableArray alloc] init];

  [self.tableData addObject:@[ @"姓名", @"任职机构", @"职位", @"工作年限", @"邮箱", @"最高学历", @"常驻城市", @"身份证明人" ]];
  [self.tableData addObject:@[ @"选择专家分类" ]];
  [self.tableData addObject:@[ @"上传身份证照片", @"" ]];
  [self.tableData addObject:@[ @"上传资质照片", @"" ]];
  [self.tableData addObject:@[ @"个人介绍", @"" ]];

  //从plist中获取地区列表的缓存数据
  NSMutableArray *arr1 = [[CacheManager shareInstance] getLocArr];
  self.locArr = [[NSMutableArray alloc] initWithArray:arr1];
  //从plist中获取年限列表的缓存数据
  NSMutableArray *arr3 = [[CacheManager shareInstance] getYearArr];
  self.yearArr = [[NSMutableArray alloc] initWithArray:arr3];
  //从plist中获取学历列表的缓存数据
  NSMutableArray *arr4 = [[CacheManager shareInstance] getEduArr];
  self.eduArr = [[NSMutableArray alloc] initWithArray:arr4];
  //从plist中获取一级分类列表的缓存数据（首页时就缓存了）
  NSMutableArray *arr2 = [[CacheManager shareInstance] getLastLevelArr];
  self.level1Arr = [[NSMutableArray alloc] initWithArray:arr2];
  //从plist中获取第一个二级分类列表的缓存数据
  if (self.level1Arr.count > 0) {
    NSString *identify = [NSString stringWithFormat:@"%@_%ld", PLIST_FILE_LAST_LEVEL2_ARR, (long) [self.level1Arr[0] levelid]];
    NSMutableArray *arr3 = [[CacheManager shareInstance] getLastLevelArrWithIdentify:identify];
    self.level2Arr = [[NSMutableArray alloc] initWithArray:arr3];
  }
}

- (void)loadData {
  //加载地区列表数据
  [LocationModel queryLocationListWithCompletion:^(NSMutableArray *locList) {
    if (locList) {
      [[CacheManager shareInstance] cacheLocArr:locList];
      [self.locArr removeAllObjects];
      self.locArr = locList;
    } else {
      NSError *error = [LocationModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
  }];

  //加载分类1列表数据
  [LevelModel queryLevel1ListWithCompletion:^(NSMutableArray *levelList) {
    if (levelList) {
      [[CacheManager shareInstance] cacheLastLevelArr:levelList];
      [self.level1Arr removeAllObjects];
      [self.level1Arr addObjectsFromArray:levelList];

      //加载分类2列表数据
      [self loadLevel2Data:self.level1Arr[0] fromPicker:NO];
    } else {
      NSError *error = [LevelModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
  }];

  //学历列表
  [TimeModel queryEducationWithCompletion:^(NSMutableArray *educationList) {
    if (educationList) {
      [[CacheManager shareInstance] cacheEduArr:educationList];
      [self.eduArr removeAllObjects];
      self.eduArr = educationList;
    } else {
      NSError *error = [TimeModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
  }];

  //年限列表
  [TimeModel queryYearWithCompletion:^(NSMutableArray *yearList) {
    if (yearList) {
      [[CacheManager shareInstance] cacheYearArr:yearList];
      [self.yearArr removeAllObjects];
      self.yearArr = yearList;
    } else {
      NSError *error = [TimeModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
  }];
}

#pragma mark - action
#pragma mark 点击头像
- (IBAction)actionOnHead:(id)sender {
  self.imageType = 0;
  [OperationManager configCameraActionSheet];
}

#pragma mark 点击下一步
- (IBAction)actionOnNext:(id)sender {
  if (self.headImage && self.idCardImageArr && self.idCardImageArr.count > 0 && self.qualificationImageArr && self.qualificationImageArr.count > 0) {

    if (self.becomeExpertModel.userName.length < 1) {
      [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请填入姓名"];
      return;
    }
    if (self.becomeExpertModel.company.length < 1) {
      [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请填入任职机构"];
      return;
    }
    if (self.becomeExpertModel.userTitle.length < 1) {
      [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请填入职位"];
      return;
    }
    if (self.becomeExpertModel.experience.length < 1) {
      [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请填入工作年限"];
      return;
    }
    if (self.becomeExpertModel.userEmail.length < 1) {
      [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请填入邮箱"];
      return;
    }
    if (![OperationManager validateEmail:self.becomeExpertModel.userEmail]) {
      [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入正常格式的邮箱"];
      return;
    }
    if (self.becomeExpertModel.education.length < 1) {
      [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请填入最高学历"];
      return;
    }
    if (self.becomeExpertModel.loc.length < 1) {
      [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请填入常驻城市"];
      return;
    }
    if (self.becomeExpertModel.witness.length < 1) {
      [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请填入身份证明人"];
      return;
    }
    if (self.becomeExpertModel.topicIndex.length < 1) {
      [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请选择专家分类"];
      return;
    }
    if (self.becomeExpertModel.topicIndex2.length < 1) {
      [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请选择专家分类"];
      return;
    }
    if (self.becomeExpertModel.selfDesc.length < 1) {
      [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请填入个人介绍"];
      return;
    }

    ThemeEditViewController *vc = [[ThemeEditViewController alloc] init];
    vc.themeEditType = ThemeEditType_BecomeExpert;
    vc.becomeExpertModel = self.becomeExpertModel;
    vc.idCardImageArr = self.idCardImageArr;
    vc.qualificationImageArr = self.qualificationImageArr;
    vc.headImage = self.headImage;
    [self.navigationController pushViewController:vc animated:YES];

  } else {
    [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请上传图片"];
  }
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return self.tableData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [[self.tableData objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return GNormalMargin; //section头部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  return 0.1; //section底部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  if (indexPath.section == 4 && indexPath.row == 1) {
    return GTextViewHeight - NavHeight;
  } else if (indexPath.row == 1 && (indexPath.section == 2 || indexPath.section == 3)) {
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
  }
  return NavHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  if (indexPath.row == 1 && (indexPath.section == 2 || indexPath.section == 3)) {
    SendWeiboImageCell *imageCell = [[[NSBundle mainBundle] loadNibNamed:@"SendWeiboImageCell" owner:nil options:nil] objectAtIndex:0];

    imageCell.row = 0;
    if (indexPath.section == 2) {
      imageCell.imageList = self.idCardImageArr;
      imageCell.type = ImageCell_IC;
    } else {
      imageCell.imageList = self.qualificationImageArr;
      imageCell.type = ImageCell_Qualification;
    }
    imageCell.cellDelegate = self;
    imageCell.selectionStyle = UITableViewCellSelectionStyleNone;

    return imageCell;
  } else {

    UITableViewCell *cell;
    if ((indexPath.section == 0 && (indexPath.row == 3 || indexPath.row == 5 || indexPath.row == 6)) || indexPath.section == 1) {
      //主副标题形式
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];

      //向右箭头，列表项点击性
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
      cell.selectionStyle = UITableViewCellSelectionStyleDefault;

      //副标题
      [CellManager configCellDetailText:cell];
      if (indexPath.section == 0) {
        if (indexPath.row == 3) {//年限
          cell.detailTextLabel.text = self.becomeExpertModel.experience ? self.becomeExpertModel.experience : @"";
        }else if (indexPath.row == 5){//学历
          cell.detailTextLabel.text = self.becomeExpertModel.education ? self.becomeExpertModel.education : @"";
        }else{//地区
          cell.detailTextLabel.text = self.becomeExpertModel.loc ? self.becomeExpertModel.loc : @"";
        }
      } else { //分类
        cell.detailTextLabel.text = self.becomeExpertModel.topicIndex ? [NSString stringWithFormat:@"%@ %@", self.becomeExpertModel.topicIndex, self.becomeExpertModel.topicIndex2] : @"";
      }
    } else {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

      //向右箭头，列表项点击性
      cell.accessoryType = UITableViewCellAccessoryNone;
      cell.selectionStyle = UITableViewCellSelectionStyleNone;

      if (indexPath.section == 0) {
        //编辑行
        UITextField *textField = [CellManager configCellNormalTextField:cell];
        textField.delegate = self;
        //避免tag默认0的设定
        textField.tag = indexPath.row + 1;
        switch (textField.tag - 1) {
          case 0:
            textField.text = self.becomeExpertModel.userName;
            break;
          case 1:
            textField.text = self.becomeExpertModel.company;
            break;
          case 2:
            textField.text = self.becomeExpertModel.userTitle;
            break;
          case 4:
            textField.text = self.becomeExpertModel.userEmail;
            break;
          case 7:
            textField.text = self.becomeExpertModel.witness;
            break;
          default:
            break;
        }
        [cell.contentView addSubview:textField];
      }
    }

    //主标题
    [CellManager configCellNormalText:cell];
    cell.textLabel.text = [[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    //编辑框
    if (indexPath.section == 4 && indexPath.row == 1) { //最后一项
      UITextView *textView = [CellManager configCellTextView:cell];
      textView.delegate = self;
      textView.text = self.becomeExpertModel.selfDesc;
      [cell.contentView addSubview:textView];
    }

    return cell;
  }
}

#pragma mark TableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES]; //返回后选中状态消失
  if (indexPath.section == 0 && indexPath.row == 3) {
    self.yearPicker = [[PickerView alloc] initWithPickerType:PickerType_Loc];
    self.yearPicker.delegate = self;
    [self.yearPicker show];
    
    //重载cell
    TimeModel *timeModel = self.yearArr[0];
    self.becomeExpertModel.experience = [NSString stringWithFormat:@"%@年", timeModel.data.work_year];
    [self reloadYearCell];
  } else if (indexPath.section == 0 && indexPath.row == 5) {
    self.eduPicker = [[PickerView alloc] initWithPickerType:PickerType_Loc];
    self.eduPicker.delegate = self;
    [self.eduPicker show];
    
    //重载cell
    TimeModel *timeModel = self.eduArr[0];
    self.becomeExpertModel.education = timeModel.data.education;
    [self reloadEduCell];
  } else if (indexPath.section == 0 && indexPath.row == 6) {
    self.locPicker = [[PickerView alloc] initWithPickerType:PickerType_Loc];
    self.locPicker.delegate = self;
    [self.locPicker show];

    //重载cell
    LocationModel *locModel = self.locArr[0];
    self.becomeExpertModel.loc = locModel.data.pro_user_location_name;
    [self reloadLocCell];
  } else if (indexPath.section == 1) {
    self.sortPicker = [[PickerView alloc] initWithPickerType:PickerType_ExpertSort];
    self.sortPicker.delegate = self;
    [self.sortPicker show];

    //重载cell
    LevelModel *level1Model = self.level1Arr[0];
    self.becomeExpertModel.topicIndex = level1Model.data.user_pro_topic_index;
    LevelModel *level2Model = self.level2Arr[0];
    self.becomeExpertModel.topicIndex2 = level2Model.data.user_pro_topic_index;
    [self reloadSortCell];
  }
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  NSString *selectStr = [actionSheet buttonTitleAtIndex:buttonIndex];
  UIImagePickerController *imagePicker = [OperationManager configImagePicker:selectStr];
  //证件图不要截取半屏显示就好了、背景需要截取
  //背景尺寸：GExpertImageHeight*屏幕宽
  if (self.imageType == 0) {
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    self.imageEditor = [[DemoImageEditor alloc] initWithNibName:@"DemoImageEditor" bundle:nil];
    self.imageEditor.checkBounds = YES;
    self.imageEditor.rotateEnabled = YES;
    self.library = library;
    
    self.imageEditor.doneCallback = ^(UIImage *editedImage, BOOL canceled){
      if(!canceled) {
        //TODO: 上传的图片可能有点不对，需要排查下
        editedImage = [[CommonMethod sharedCommonMethod] compressImage:editedImage]; //压缩图片
        self.headImage = editedImage;
        [self.headBtn setImage:editedImage forState:UIControlStateNormal];
      }
      if (imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        [self.navigationController popViewControllerAnimated:NO];
        [self.navigationController setNavigationBarHidden:NO animated:YES];
      }else{
        [self.imageEditor dismissViewControllerAnimated:YES completion:nil];
        [imagePicker dismissViewControllerAnimated:YES completion:nil];
      }
      
    };
  }

  if (!imagePicker) {
    return;
  }
}

#pragma mark UIImagePickerControllerDelegate 选照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info {

  //选择照片
  UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
  originalImage = [[CommonMethod sharedCommonMethod] compressImage:originalImage]; //压缩图片
  //三种情况：头像、身份证图片列表、资质图片列表
  if (self.imageType == ImageCell_IC) {
    [self.idCardImageArr addObject:originalImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
    
  } else if (self.imageType == ImageCell_Qualification) {
    [self.qualificationImageArr addObject:originalImage];
    [picker dismissViewControllerAnimated:YES completion:nil];

  } else {
    
    UIImage *image =  [info objectForKey:UIImagePickerControllerOriginalImage];
    NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    
    [self.library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
      UIImage *preview = [UIImage imageWithCGImage:[asset aspectRatioThumbnail]];
      
      self.imageEditor.sourceImage = image;
      self.imageEditor.previewImage = preview;
      [self.imageEditor reset:NO];
      if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        [picker dismissViewControllerAnimated:NO completion:nil];
        [self.navigationController pushViewController:self.imageEditor animated:NO];
        [self.navigationController setNavigationBarHidden:YES animated:NO];
      }else{
        [picker presentViewController:self.imageEditor animated:YES completion:nil];
      }
      
    } failureBlock:^(NSError *error) {
      NSLog(@"Failed to get asset from library");
    }];

  }

  [self.tableView reloadData];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark Picker Data Source Method
// 返回列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  if ([pickerView isEqual:self.sortPicker.pickerView]) { //专家分类
    return 2;
  } else { //地区or年限or学历
    return 1;
  }
}

// 返回该列行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  if ([pickerView isEqual:self.sortPicker.pickerView]) { //专家分类
    if (component == 0) {
      return self.level1Arr.count;
    } else {
      return self.level2Arr.count;
    }
  } else if ([pickerView isEqual:self.yearPicker.pickerView]) { //年限分类
    return self.yearArr.count;
  } else if ([pickerView isEqual:self.eduPicker.pickerView]) { //学历分类
    return self.eduArr.count;
  } else { //地区
    return self.locArr.count;
  }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  if ([pickerView isEqual:self.sortPicker.pickerView]) { //专家分类

    if (component == 0) {
      LevelModel *level1 = [self.level1Arr objectAtIndex:row];
      self.becomeExpertModel.topicIndex = level1.data.user_pro_topic_index;
      //加载分类2的数据
      UIWindow *window = [UIApplication sharedApplication].keyWindow;
      [[CommonMethod sharedCommonMethod] createProgressHUDInView:window withMessage:@""];
      [self loadLevel2Data:level1 fromPicker:YES];
    } else {
      LevelModel *level2 = [self.level2Arr objectAtIndex:row];
      self.becomeExpertModel.topicIndex2 = level2.data.user_pro_topic_index;
    }

    //重载cell
    [self reloadSortCell];
  } else if ([pickerView isEqual:self.yearPicker.pickerView]) { //年限分类
    TimeModel *year = [self.yearArr objectAtIndex:row];
    self.becomeExpertModel.experience = [NSString stringWithFormat:@"%@年", year.data.work_year];
    
    //重载cell
    [self reloadYearCell];
  } else if ([pickerView isEqual:self.eduPicker.pickerView]) { //学历分类
    TimeModel *edu = [self.eduArr objectAtIndex:row];
    self.becomeExpertModel.education = edu.data.education;
    
    //重载cell
    [self reloadEduCell];
  } else { //地区
    LocationModel *loc = [self.locArr objectAtIndex:row];
    self.becomeExpertModel.loc = loc.data.pro_user_location_name;

    //重载cell
    [self reloadLocCell];
  }
}

#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
  if ([pickerView isEqual:self.sortPicker.pickerView]) { //专家分类
    if (component == 0) {
      LevelModel *level1 = [self.level1Arr objectAtIndex:row];
      return level1.data.user_pro_topic_index;
    } else {
      LevelModel *level2 = [self.level2Arr objectAtIndex:row];
      return level2.data.user_pro_topic_index;
    }
  } else if ([pickerView isEqual:self.yearPicker.pickerView]) { //年限分类
    TimeModel *year = [self.yearArr objectAtIndex:row];
    return [NSString stringWithFormat:@"%@年", year.data.work_year];
  } else if ([pickerView isEqual:self.eduPicker.pickerView]) { //学历分类
    TimeModel *edu = [self.eduArr objectAtIndex:row];
    return edu.data.education;
  } else { //地区
    LocationModel *loc = [self.locArr objectAtIndex:row];
    return loc.data.pro_user_location_name;
  }
}

#pragma mark PickerViewDelegate
- (void)cancelSelect:(UIView *)picker{
  if (picker == self.yearPicker) {
    self.becomeExpertModel.experience = @"";
    [self reloadYearCell];
  }else if (picker == self.eduPicker){
    self.becomeExpertModel.education = @"";
    [self reloadEduCell];
  }else if (picker == self.locPicker){
    self.becomeExpertModel.loc = @"";
    [self reloadLocCell];
  }else if (picker == self.sortPicker){
    self.becomeExpertModel.topicIndex = @"";
    self.becomeExpertModel.topicIndex2 = @"";
    [self reloadSortCell];
  }
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
  self.becomeExpertModel.selfDesc = textView.text;
}

#pragma mark - SendWeiboImage delegate
#pragma mark 删除照片
- (void)sendWeiboImage:(SendWeiboImage *)imageView deleteAtIndex:(NSInteger)index type:(ImageCell_Type)type {

  if (type == ImageCell_IC) {
    NSMutableArray *imgsMArray = [self.idCardImageArr mutableCopy];
    [imgsMArray removeObjectAtIndex:index];
    [self.idCardImageArr removeAllObjects];
    [self.idCardImageArr addObjectsFromArray:imgsMArray];
  } else {
    NSMutableArray *imgsMArray = [self.qualificationImageArr mutableCopy];
    [imgsMArray removeObjectAtIndex:index];
    [self.qualificationImageArr removeAllObjects];
    [self.qualificationImageArr addObjectsFromArray:imgsMArray];
  }

  [self.tableView reloadData];
}

#pragma mark 添加照片
- (void)clickToAddImageWithType:(ImageCell_Type)type {
  self.imageType = type;
  [OperationManager configCameraActionSheet];
}

#pragma mark - 私有方法
#pragma mark textfield中的文本发生改变时
- (void)textFieldChanged:(NSNotification *)notification {
  UITextField *textField = (UITextField *) notification.object;
  //专家编辑页进入到话题编辑页后，也会有textfield的编辑监控。所以加此判断，过滤掉话题编辑页过来的编辑。
  if (textField.tag) {
    switch (textField.tag - 1) {
      case 0:
        self.becomeExpertModel.userName = textField.text;
        break;
      case 1:
        self.becomeExpertModel.company = textField.text;
        break;
      case 2:
        self.becomeExpertModel.userTitle = textField.text;
        break;
      case 4:
        self.becomeExpertModel.userEmail = textField.text;
        break;
      case 7:
        self.becomeExpertModel.witness = textField.text;
        break;
      default:
        break;
    }
  }
}

#pragma mark 加载分类2列表数据
- (void)loadLevel2Data:(LevelModel *)level1 fromPicker:(BOOL)fromPicker {
  //加载分类2的数据
  [LevelModel queryLevel2ListWithLevel1Id:level1.levelid completion:^(NSMutableArray *levelList) {
    if (levelList) {
      NSString *identify = [NSString stringWithFormat:@"%@_%ld", PLIST_FILE_LAST_LEVEL2_ARR, (long) level1.levelid];
      [[CacheManager shareInstance] cacheLastLevel2Arr:levelList identify:identify];
      [self.level2Arr removeAllObjects];
      [self.level2Arr addObjectsFromArray:levelList];
      if (fromPicker) {
        [self.sortPicker.pickerView reloadAllComponents];
      }
    } else {
      NSError *error = [LevelModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [MBProgressHUD hideAllHUDsForView:window animated:YES];
  }];
}

#pragma mark 重载年限cell
- (void)reloadYearCell {
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
  [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark 重载学历cell
- (void)reloadEduCell {
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:5 inSection:0];
  [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark 重载地区cell
- (void)reloadLocCell {
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:6 inSection:0];
  [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark 重载分类cell
- (void)reloadSortCell {
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
  [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
}

@end
